#read one GEOtop time series file
TimeSeriesReadOne <- function(infile, comment = comment, env = env) {
  #count comment lines
  found <- FALSE #begin of data found? (first line without comment)
  fs <- readLines(infile, n = 1000)
  for (l in 1:length(fs)) {
    line <- str_trim(fs[l], side = "both")
    if (substr(line, 1, 1) != env$comchar) break
  }
  l<-l-1
  
  #decide if comment of data is returned
  if (comment == TRUE) {
    return(fs[1:l]) #get and return comment
  }

  #if no comment is to be returned read and process data, avoid empty files
  data <- read.csv(infile, skip = l, header = TRUE, sep = ",", dec = ".")
  if (nrow(data) == 0) {
	stop("No valid data rows in this file.")
  }
 
  data$Date <- as.POSIXct(data[,1], env$timefmt, tz="UTC")
  if (sum(is.na(data$Date)) > 0) {
  	stop("One or more elements of the first column are invalid dates.")
  }
 
  #fix nodata and return
  data[data == env$nodata] <- NA
  return(data)
}